package user

import (
	"net/http"
	"time"
	"vite-tsc-gin/api"
	"vite-tsc-gin/pkg/log"
	"vite-tsc-gin/pkg/utils"

	"github.com/gin-gonic/gin"
)

func (u *UserController) Create(c *gin.Context) {
	log.Info("user create function called.")

	var r api.User
	if err := c.ShouldBindJSON(&r); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 1,
			"msg":  err.Error(),
		})
		return
	}

	r.Password, _ = utils.Encrypt(r.Password)
	r.Status = 1
	r.LoginedAt = time.Now()

	if err := u.srv.Users().Create(c, &r); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 1,
			"msg":  err.Error(),
		})

	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
		})
	}

}
